<?php

/**
 * @Description:类别管理控制器
 * @author Cloud <190296465@qq.com>
 * @since 2014-5-27 16:40:45
 * @copyright ChofnERP
 */

namespace Admin\Controller;

use Common\Model\NewsModel;

class NewsController extends AdminController {

    function index() {
        parent::indexInitialize();
        $categoryModel = new \Common\Model\CategoryModel();
        $cateData = $categoryModel->where($this->map)->order('sort asc')->select();
        $treeModel = new \Common\Org\Tree($cateData);
        $cateData = $treeModel->getArray();
        $this->assign('cateData', $cateData);
        parent::commonIndex();
    }

    function jsonList() {
        parent::indexInitialize();
        $model = new NewsModel();

        $pageNo = I('get.page', '1', 'intval');
        $pagesize = I('get.limit', $this->pagesize, 'intval');
        $pageNum = ($pageNo - 1) * $pagesize;
        $limit = "{$pageNum},{$pagesize}";

        if (I('get.category', '', 'intval'))
            $this->map['category'] = array('EQ', I('get.category'));
        if (I('get.wd', '', 'trim') && $model->_selectFields)
            $this->map[$model->_selectFields] = array('LIKE', '%' . I('get.wd') . '%');

        $count = $model->where($this->map)->count();

        //$pager = new \Common\Org\Pager();
        //$pagerData = $pager->getPagerData($count, $pageNo, $url, 2, $this->pagesize); //参数记录数 当前页数 链接地址 显示样式 每页数量

        $data = $model->where($this->map)->order($this->order)->limit($limit)->select();


        //$data = $model->select();
        $this->ajaxReturn([
            'code' => 0, 'msg' => 'success', 'count' => $count, 'data' => $data
        ]);
    }

    function add() {
        parent::indexInitialize();
        $menuModel = new \Common\Model\MenuModel();
        $menuData = $menuModel->find($this->menu_id);
        if (in_array('noadd', $menuData['allow_field']) && !I('get.id'))
            $this->error('本栏目禁止添加新纪录!');
        if (in_array('nomod', $menuData['allow_field']))
            $this->error('本栏目禁止修改纪录!');
        parent::commonAdd();
    }

    function save() {
        parent::commonSave();
    }

    function delete() {
        parent::indexInitialize();
        $menuModel = new \Common\Model\MenuModel();
        $menuData = $menuModel->find($this->menu_id);
        if (in_array('nodel', $menuData['allow_field']))
            $this->error('本栏目禁止删除纪录!');
        parent::commonDelete();
    }

    function export() {
        $model = new \Common\Model\NewsModel();
        Vendor('PHPExcel');
        Vendor('PHPExcel.IOFactory');
        Vendor('PHPExcel.Reader.Excel5');
        $objPHPExcel = new \PHPExcel();
        // Set properties    
        $objPHPExcel->getProperties()->setCreator("MY.PHP.CMS")
                ->setLastModifiedBy("MY.PHP.CMS")
                ->setTitle("Office 2007 XLSX Test Document")
                ->setSubject("Office 2007 XLSX Test Document")
                ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                ->setKeywords("office 2007 openxml php")
                ->setCategory("Test result file");

        //set width    
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(25);
        //set height    
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
        $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(25);
        $objPHPExcel->getActiveSheet()->getRowDimension('3')->setRowHeight(25);
        $objPHPExcel->getActiveSheet()->getRowDimension('4')->setRowHeight(25);

        //set style
        $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(9);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);

        //set algin
        $objPHPExcel->getActiveSheet()->getStyle('A1:D20')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A1:D20')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

        //set border
        $objPHPExcel->getActiveSheet()->getStyle('A1:D4')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);

        //单元格合并
        $objPHPExcel->getActiveSheet()->mergeCells('A1:D1');
        $objPHPExcel->getActiveSheet()->mergeCells('A2:A4');

        //写入内容
        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', 'A1')
                ->setCellValue('A2', 'A2');

        /*
          foreach ($data['income'] as $k => $v) {
          $objPHPExcel->getActiveSheet()->getRowDimension(($k + 8))->setRowHeight(25);
          $objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($k + 8), '收');
          $objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($k + 8), $v['title2']);
          $objPHPExcel->getActiveSheet(0)->setCellValue('D' . ($k + 8), date('Y-m-d H:i', $v['created']));
          }
          foreach ($data['cost'] as $k => $v) {
          $objPHPExcel->getActiveSheet()->getRowDimension(($k + 8 + count($data['income'])))->setRowHeight(25);
          $objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($k + 8 + count($data['income'])), '支');
          $objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($k + 8 + count($data['income'])), $v['categoryHtml']);
          $objPHPExcel->getActiveSheet(0)->setCellValue('C' . ($k + 8 + count($data['income'])), $v['title3']);
          $objPHPExcel->getActiveSheet(0)->setCellValue('D' . ($k + 8 + count($data['income'])), date('Y-m-d H:i', $v['created']));
          } */

        //设置->标题
        $objPHPExcel->getActiveSheet(0)->setTitle('工作表');

        //创建新工作表
        $msgWorkSheet = new \PHPExcel_Worksheet($objPHPExcel, '工作表1'); //创建一个工作表
        $objPHPExcel->addSheet($msgWorkSheet); //插入工作表
        //$objPHPExcel->setActiveSheetIndex(1); //切换到新创建的工作表
        //设置活动表单
        $objPHPExcel->setActiveSheetIndex(0);

        //从浏览器直接输出$filename
        $filename = '/' . $data['title'] . '(' . date('YmdHis') . ')' . '结算单.xls';
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type: application/vnd.ms-excel;");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=" . $filename);
        header("Content-Transfer-Encoding:binary");
        $objWriter->save("php://output");
        //输出PDF格式  
        /*
          $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
          $objWriter->setSheetIndex(0);
          header("Pragma: public");
          header("Expires: 0");
          header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
          header("Content-Type:application/force-download");
          header("Content-Type: application/pdf");
          header("Content-Type:application/octet-stream");
          header("Content-Type:application/download");
          header("Content-Disposition:attachment;filename=" . $filename);
          header("Content-Transfer-Encoding:binary");
          $objWriter->save("php://output"); */
    }

}

?>